package com.cbn.ibcp.web.jishiting.operator.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.cbn.ibcp.common.annotation.Log;
import com.cbn.ibcp.common.core.domain.Result;
import com.cbn.ibcp.common.enums.BusinessType;
import com.cbn.ibcp.jishiting.rms.domain.ResBase;
import com.cbn.ibcp.jishiting.rms.domain.ResBasePlayConfig;
import com.cbn.ibcp.jishiting.rms.domain.ResCp;
import com.cbn.ibcp.jishiting.rms.domain.ResourceStreamOrg;
import com.cbn.ibcp.jishiting.rms.service.IResBasePlayConfigService;
import com.cbn.ibcp.jishiting.rms.service.IResBaseService;
import com.cbn.ibcp.jishiting.rms.service.IResCpService;
import com.cbn.ibcp.jishiting.rms.service.IResourceStreamOrgService;
import com.cbn.ibcp.web.client.DataInjectionClient;
import com.cbn.ibcp.web.client.dto.ResBaseDTO;
import com.cbn.ibcp.web.client.dto.ResourceStreamOrgDTO;
import com.cbn.ibcp.web.common.config.GsCmsConfig;
import com.cbn.ibcp.web.init.LoadDatasource;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dataInjection")
@RequiredArgsConstructor
public class DataInjectionControler {


    private final DataInjectionClient dataInjectionClient;

    private final IResBaseService resBaseService;

    private final IResCpService resCpService;

    private final IResBasePlayConfigService resBasePlayConfigService;

    private final IResourceStreamOrgService resourceStreamOrgService;

    @PreAuthorize("@ss.hasAnyPermi('jishiting:rms:resourcestream:origin:videotrigger')")
    @Log(title = "源站注入", businessType = BusinessType.UPDATE)
    @GetMapping("/originVideoTrigger")
    public Result<String> originVideoTrigger(Boolean reinject, String videoId) {
        String stringResult = dataInjectionClient.originVideoTrigger(reinject, videoId);
        return Result.ok(stringResult);
    }


    @PreAuthorize("@ss.hasAnyPermi('jishiting:rms:resourcestream:origin:province:videotrigger')")
    @Log(title = "源站分省注入", businessType = BusinessType.UPDATE)
    @GetMapping("/originProvinceVideoTrigger")
    public Result<String> originProvinceVideoTrigger(String orgCodesStr, Boolean reinject, Long videoId) {
        //查询注入省份
        List<ResBase> resBaseList = resBaseService.getListByIdListAndResTypeList(ListUtil.toList(videoId), ListUtil.toList(1, 21));
        if (CollectionUtil.isEmpty(resBaseList)) {
            return Result.ok("未找到资源");
        }
        ResBase resBase = resBaseList.get(0);
        Long contentProviderId = resBase.getContentProviderId();
        ResCp resCp = resCpService.getById(contentProviderId);
        String code = resCp.getCode();
        List<ResBasePlayConfig> resBasePlayConfigList = resBasePlayConfigService.getListByCpCode(code);
        if (CollectionUtil.isEmpty(resBasePlayConfigList)) {
            return Result.ok("未找到分省配置");
        }
        List<String> orgCodeList = resBasePlayConfigList.stream().map(ResBasePlayConfig::getOrgCode).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(orgCodeList)) {
            return Result.ok("未找到分省编码");
        }
        List<String> orgCodes = null;
        if (StrUtil.isNotBlank(orgCodesStr)) {
            orgCodes = StrUtil.split(orgCodesStr, ",");
        }
        if (CollectionUtil.isNotEmpty(orgCodes)) {
            orgCodeList = orgCodeList.stream().filter(Objects::nonNull).filter(orgCodes::contains).collect(Collectors.toList());
        }
        //过滤掉不能注入的orgCode
        List<String> cdnOrgCodeList = GsCmsConfig.cdnOrgCodeList;
        orgCodeList.removeIf(orgCode -> !cdnOrgCodeList.contains(orgCode));
        orgCodeList = orgCodeList.stream().filter(tmpOrgCode -> !LoadDatasource.notInjectOrgCodeList.contains(tmpOrgCode)).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(orgCodeList)) {
            return Result.ok("未找到分省编码");
        }
        orgCodeList.forEach(tmpOrgCode -> dataInjectionClient.provinceVideoTrigger(tmpOrgCode, reinject, StrUtil.toStringOrNull(videoId)));
        return Result.ok();
    }

    @PreAuthorize("@ss.hasAnyPermi('jishiting:rms:resourcestream:province:videotrigger,jishiting:rms:resourcestreamorg:province:videotrigger')")
    @Log(title = "分省注入", businessType = BusinessType.UPDATE)
    @GetMapping("/provinceVideoTrigger")
    public Result<String> provinceVideoTrigger(String orgCode, Boolean reinject, String videoId) {
        if (StrUtil.isBlank(orgCode) || Objects.isNull(reinject) || StrUtil.isBlank(videoId)) {
            return Result.fail("参数错误");
        }
        String stringResult = dataInjectionClient.provinceVideoTrigger(orgCode, reinject, videoId);
        return Result.ok(stringResult);
    }


    @PreAuthorize("@ss.hasAnyPermi('jishiting:rms:resourcestream:origin:modifyinjectstatus')")
    @Log(title = "修改源站注入状态", businessType = BusinessType.UPDATE)
    @GetMapping("/modifyOriginInjectStatus")
    public Result<String> modifyOriginInjectStatus(String albumId, String videoId, Integer injectStatus) {
        if (StrUtil.isBlank(albumId) || Objects.isNull(videoId)) {
            return Result.fail("参数错误");
        }
        Long videoIdLong = Long.parseLong(videoId);
        String resourceStreamResult = dataInjectionClient.modifyOriginInjectStatus(videoId, injectStatus);
        ResBaseDTO resBaseDTO = new ResBaseDTO();
        resBaseDTO.setId(videoIdLong);
        resBaseDTO.setInjectStatus(injectStatus);
        String resBaseResult = dataInjectionClient.modifyOriginResBase(resBaseDTO);
        return Result.ok(resourceStreamResult + "\t" + resBaseResult);
    }


    @PreAuthorize("@ss.hasAnyPermi('jishiting:rms:resourcestream:province:modifyProvinceResourceStreamOrg')")
    @Log(title = "修改分省(org)注入状态", businessType = BusinessType.UPDATE)
    @GetMapping("/modifyProvinceResourceStreamOrg")
    public Result<String> modifyProvinceResourceStreamOrg(String orgCode, String albumId, String videoId, Integer injectStatus) {
        if (StrUtil.isBlank(albumId) || Objects.isNull(videoId)) {
            return Result.fail("参数错误");
        }
        Long videoIdLong = Long.parseLong(videoId);
        ResourceStreamOrg resourceStreamOrg = resourceStreamOrgService.selectOneByVideoId(orgCode, orgCode, videoIdLong);
        if (Objects.isNull(resourceStreamOrg)) {
            return Result.fail("未找到该资源");
        }
        ResourceStreamOrgDTO resourceStreamOrgDTO = new ResourceStreamOrgDTO();
        resourceStreamOrgDTO.setOrgCode(orgCode);
        resourceStreamOrgDTO.setStreamId(resourceStreamOrg.getStreamId());
        resourceStreamOrgDTO.setInjectStatus(injectStatus);
        String resourceStreamResult = dataInjectionClient.modifyProvinceResourceStreamOrg(resourceStreamOrgDTO);
        return Result.ok(resourceStreamResult + "\t" + resourceStreamResult);
    }
}
